package com.example.xd.commen_utils.installpackageinfo

import android.content.Context
import android.content.pm.ApplicationInfo.FLAG_SYSTEM
import android.content.pm.PackageInfo

/**
 * @auther XuDan on 2020/9/1.
 */
object InstallPackageInfoUtil {
    /**
     * 返回当前用户已安装的所有包的列表
     */
    fun getAllApps(context: Context): MutableList<PackageInfo> {
        val installedPackages = context.packageManager.getInstalledPackages(0)
        return installedPackages
    }

    /**
     * 获取所有系统应用
     */
    fun getSystemApps(context: Context): MutableList<PackageInfo> {
        var systemApps = mutableListOf<PackageInfo>()
        val allApps = getAllApps(context)
        for (app in allApps) {
            val flags = app.applicationInfo.flags
            if (flags === FLAG_SYSTEM) {
                systemApps.add(app)
            }
        }
        return systemApps

    }

    /**
     * 获取所有非系统应用
     */
    fun getNoSystemApps(context: Context): MutableList<PackageInfo> {
        var listApps = mutableListOf<PackageInfo>()
        val allApps = getAllApps(context)
        for (app in allApps) {
            if (!(app.applicationInfo.flags and FLAG_SYSTEM == 0)) {
                listApps.add(app)
            }
        }
        return listApps

    }
}